﻿//根据百度经纬度坐标获取地址信息
function getAddressInfo(lat, lng, callback) {
    url = 'http://api.map.baidu.com/geocoder/v2/?ak=UC03PvVN1AEqsb6csSzY6vIF&location=' + Number(lat) + ',' + Number(lng) + '&output=json&callback=?';
    $.getJSON(url, function (msg) {
        //回调函数
        if (callback) {
            callback(msg);
        }
    });
}
//腾讯坐标转百度坐标
//lat-经度
//lng-纬度
//callback-回调函数
function ConvertTencentToBaidu(lat, lng, callback) {
    url = 'http://api.map.baidu.com/geoconv/v1/?coords=' + Number(lat) + ',' + Number(lng) + '&from=3&to=5&output=json&ak=UC03PvVN1AEqsb6csSzY6vIF&callback=?';
    $.getJSON(url, function (msg) {
        if (callback) {
            callback(msg);
        }
    });
}

///函数说明：点击定位到当前位置
///lat-纬度
///lng-经度
function locationPoint(url) {
    var Latitude = AddressMgr.getWXLat();
    var Longitude = AddressMgr.getWXLog();
    ConvertTencentToBaidu(Latitude, Longitude, function (msg) {
        if (msg.status == 0) {
            var myObj = eval(msg.result);
            var Latitude = myObj[0].x;
            var Longitude = myObj[0].y;
            //根据百度坐标获取地址信息
            getAddressInfo(Latitude, Longitude, function (msg) {
                if (msg.status == 0) {
                    var addressName = msg.result.formatted_address;
                    var addressInfo = msg.result.formatted_address;
                    var myObj = eval(msg.result.poiRegions);
                    if (myObj.length > 0) {
                        addressName = myObj[0].name;
                    }
                    AddressMgr.setWXAddr(addressName, addressInfo, Latitude, Longitude); 
                    AddressMgr.setCurAddr(addressName, addressInfo, Latitude, Longitude);
                    //定位之后跳转到首页
                    window.location.href = url;
                }
            });
        }
    });
}

var AddressMgr = {
    addressFromWX: {
        name: "ss.add.wx.name",
        addr: "ss.add.wx.addr",
        lat: "ss.add.wx.lat",
        log: "ss.add.wx.log",
    },
    addressCur: {
        name: "ss.add.cur.name",
        addr: "ss.add.cur.addr",
        lat: "ss.add.cur.lat",
        log: "ss.add.cur.log",
    },
    serviceCity: {
        list: "ss.add.city.list",
    },
    setWXAddr: function (name, addr, lat, log) {
        if (name != null) {
            setStorage(AddressMgr.addressFromWX.name, name);
        }
        if (addr != null) {
            setStorage(AddressMgr.addressFromWX.addr, addr);
        }
        setStorage(AddressMgr.addressFromWX.lat, lat);
        setStorage(AddressMgr.addressFromWX.log, log);
    },
    setCurAddr: function (name, addr, lat, log) {
        setStorage(AddressMgr.addressCur.name, name);
        setStorage(AddressMgr.addressCur.addr, addr);
        setStorage(AddressMgr.addressCur.lat, lat);
        setStorage(AddressMgr.addressCur.log, log);
    },
    useWXAddr: function () {
        var name = getStorageVal(AddressMgr.addressFromWX.name);
        setStorage(AddressMgr.addressCur.name, name);
        var addr = getStorageVal(AddressMgr.addressFromWX.addr);
        setStorage(AddressMgr.addressCur.addr, addr);
        var lat = getStorageVal(AddressMgr.addressFromWX.lat);
        setStorage(AddressMgr.addressCur.lat, lat);
        var log = getStorageVal(AddressMgr.addressFromWX.log);
        setStorage(AddressMgr.addressCur.log, log);
    },
    getWXLat: function() {
        return getStorageVal(AddressMgr.addressFromWX.lat);
    },
    getWXLog: function() {
        return getStorageVal(AddressMgr.addressFromWX.log);
    },
    getCurName: function () {
        return getStorageVal(AddressMgr.addressCur.name);
    },
    getCurAddr: function () {
        return getStorageVal(AddressMgr.addressCur.addr);
    },
    getCurLat: function () {
        return getStorageVal(AddressMgr.addressCur.lat);
    },
    getCurLog: function () {
        return getStorageVal(AddressMgr.addressCur.log);
    },
    getCityList: function () {
        return getStorageVal(AddressMgr.serviceCity.list);
    },
    setCityList: function (cityList) {
        setStorage(AddressMgr.serviceCity.list, cityList);
    },
    getGPSRequestJson: function () {
        var lat = AddressMgr.getCurLat();
        var log = AddressMgr.getCurLog();
        var address = AddressMgr.getCurAddr();
        var addressName = AddressMgr.getCurName();
        var GPSRequestJson = {
            "gps_info": {
                "latitude": lat,
                "longitude": log
            }, // GPS信息
            "address": address, // 地址信息
            "addr_name": addressName // 地址名称
        };
        return GPSRequestJson;
    },
};